#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 14-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
#endregion

namespace Demo
{
    /// <summary>
    /// A simple example of security provider.
    /// </summary>
#if DEBUG
    public
#else
    internal
#endif
 class SecurityModel
    {
        #region Is Authenticated
        private bool mIsAuthenticated;
        /// <summary>
        /// Checks whether the current user has logged in
        /// </summary>
        public static bool IsAuthenticated
        {
            get { return Instance.mIsAuthenticated; }
        }
        #endregion

        #region Role
        private UserRole mRole = UserRole.User;
        /// <summary>
        /// Gets role of the current user.
        /// </summary>
        public static UserRole Role
        {
            get { return Instance.mRole; }
        }
        #endregion

        #region Login
        /// <summary>
        /// A very crude example of signing in a user.
        /// </summary>
        public static bool Login(string username, string password)
        {
            return Instance.DoLogin(username, password);
        }

        bool DoLogin(string username, string password)
        {
            bool result = false;

            if ((username == "admin") && (password == "admin"))
            {
                mRole = UserRole.Admin;
                mIsAuthenticated = true;
                result = true;
            }
            else if ((username == "user") && (password == "user"))
            {
                mRole = UserRole.User;
                mIsAuthenticated = true;
                result = true;
            }
            else
            {
                mRole = UserRole.User;
                mIsAuthenticated = false;
                result = false;
            }

            return result;
        }
        #endregion

        #region Log Off
        /// <summary>
        /// Sign off current user.
        /// </summary>
        public static void LogOff()
        {
            Instance.DoLogOff();
        }

        void DoLogOff()
        {
            mRole = UserRole.User;
            mIsAuthenticated = false;
        }
        #endregion

        #region Instance
        /// <summary>
        /// Gets an instance of <see cref="type"/>
        /// </summary>
        static SecurityModel Instance
        {
            get
            {
                /// An instance of Singleton wont be created until the very first 
                /// call to the sealed class. This a CLR optimization that ensure that
                /// we have properly lazy-loading singleton. 
                return SecurityModelCreator.CreatorInstance;
            }
        }

        /// <summary>
        /// Sealed class to avoid any heritage from this helper class
        /// </summary>
        private sealed class SecurityModelCreator
        {
            // Retrieve a single instance of a Singleton
            private static readonly SecurityModel mInstance = new SecurityModel();

            /// <summary>
            /// Return an instance of the class <see cref="SecurityModel"/>
            /// </summary>
            public static SecurityModel CreatorInstance
            {
                get { return mInstance; }
            }
        }
        #endregion

        #region Private constructor
        /// <summary>
        /// Private constructor to avoid other instantiation
        /// This must be present otherwise the compiler provide 
        /// a default public constructor
        /// </summary>
        private SecurityModel()
        { }
        #endregion
    }

    public enum UserRole
    {
        Admin,
        User
    }
}
